package android.meetingatsb.common

import android.app.Activity
import android.content.Context


class Loader {
    companion object {
        private fun saveData(context: Context, key: String, cotent: String) {
            val saveHelper = context
                    .getSharedPreferences("user_information", Activity.MODE_PRIVATE)
                    .edit()

            saveHelper.putString(key, cotent)
            saveHelper.apply()
        }

        private fun readData(context: Context, key: String): String? {
            val readHelper = context.getSharedPreferences("user_information", Activity.MODE_PRIVATE)
            return readHelper.getString(key, null)
        }

        fun getUser(context: Context): User? {
            val name = readData(context, "name")
            val group = readData(context, "group")
            val university = readData(context, "university")
            val age = readData(context, "age")?.toInt()
            val gender = readData(context, "gender")
            val rating = readData(context, "rating")?.toFloat()
            return if (name == null) null else User(name!!, group!!,university!!,age!!,gender!!,rating!!)
        }

        fun saveUser(context: Context, user: User) {
            saveData(context, "name", user.name)
            saveData(context, "group", user.name)
            saveData(context, "university", user.name)
            saveData(context, "age", user.name)
            saveData(context, "gender", user.name)
            saveData(context, "rating", user.name)
        }
    }
}